/*
 * Copyright (c) 2022 Kirky
 * Licensed under the Open Software Licence, Version 3.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     https://opensource.org/licenses/osl-3.0.php
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.lingeringdream.luciole.helper.list;

import com.lingeringdream.luciole.ResourceTable;
import com.lingeringdream.luciole.slice.DetailSlice;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;

import java.util.ArrayList;
import java.util.List;

/**
 * String 类型数据封装器
 *
 * @author Kirky
 * @version 1.2
 * @date 2022/5/1
 */
public class TabListItemProvider extends BaseItemProvider {
    private final List<String> contents = new ArrayList<>();
    private final List<Boolean> arrows = new ArrayList<>();
    private final AbilitySlice slice;
    private final boolean isChinese;

    public TabListItemProvider(AbilitySlice slice, boolean isChinese) {
        this.slice = slice;
        this.isChinese = isChinese;
    }

    public void addItem(String value, boolean arrow) {
        if (value != null && !"".equals(value)) {
            contents.add(value);
            arrows.add(arrow);
        }
    }

    public void addItem(String value) {
        addItem(value, true);
    }


    @Override
    public int getCount() {
        if (contents.size() == arrows.size()) {
            return contents.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        if (position >= 0 && position < getCount()) {
            if (contents.size() > 0) {
                return contents.get(position);
            }
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        if (position >= 0 && position < getCount()) {
            return position;
        }
       return 0;
    }

    @Override
    public Component getComponent(int position, Component convertComponent, ComponentContainer componentContainer) {
        final Component component;
        if (convertComponent == null) {
            component = LayoutScatter.getInstance(slice).parse(ResourceTable.Layout_layout_tablist_item, null, false);
        } else {
            component = convertComponent;
        }

//        设置内容
        if (getCount() > 0) {
            Text content = component.findComponentById(ResourceTable.Id_tabList_item_content);
            content.setText(contents.get(position));
            component.setVisibility(Component.VISIBLE);
        }
//        设置arrow
        if (!arrows.get(position)) {
            component.findComponentById(ResourceTable.Id_tabList_item_arrow)
                    .setVisibility(Component.HIDE);
        }

//        设置监听
        component.setClickedListener(component1 -> {
            String content = this.contents.get(position);
//            跳转详情页
            Intent intent = new Intent();
            intent.setParam("key", content);
            intent.setParam("isChinese", isChinese);
            slice.present(new DetailSlice(), intent);
        });

        return component;
    }

}
